package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.entity.MemberReport;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 会员SERVICE实现类
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public MemberReport getMemberReport() {

        MemberReport memberReport = new MemberReport();

        //存月份和当前月的会员数量
        String[] months = new String[12];
        Integer[] memberCount = new Integer[12];
       // List<String> months = new ArrayList<>();
      //  List<Integer>  memberCount =new ArrayList<>();
        //获取当前日期
        Date date = new Date();
        Integer month = Integer.valueOf(String.format("%tm", date));
        Integer year = Integer.valueOf(String.format("%tY", date));

        Integer sumReport  = null;

        Calendar calStart = Calendar.getInstance();
        Calendar calEnd = Calendar.getInstance();

        //遍历12个月
        for (int i=12;month>=0 &&i>0;i--,month--){
            if (month>0){
                //日历类获取设置当前月的起始日期
                calStart.set(year,month-1,1);
                //将起始日期 从日期类型转换到String类型 并按格式存入数组中
                 Date  dateCalendar = calStart.getTime();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String start = format.format(dateCalendar);
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");
                String yearAndMonths = format1.format(dateCalendar);
                months[i-1] =yearAndMonths;
            // months.add(start);
                //将当月最后一天  从日期类型转换到String类型 并按格式存入数组中
                calEnd.set(year,month,1);
                //(7)Calendar对象cal调用方法把天数减1,之后就是2月份的最后一天
                calEnd.add(Calendar.DAY_OF_MONTH, -1);
                dateCalendar = calEnd.getTime();
                String end = format.format(dateCalendar);
                //获取当前月的会员集合
                Integer members =  memberDao.getMember(start,end);
              //  memberCount.add(sumReport);
                //查询之前的会员数量
                sumReport  =  memberDao.getMemberReport(start);
                //存入之前的会员数量
                memberCount[i-1]=sumReport+members;
            }else {
                month+=12;
                year=year-1;
                calStart.set(year,month-1,1);
                Date  dateCalendar = calStart.getTime();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String start = format.format(dateCalendar);
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");
                String yearAndMonths = format1.format(dateCalendar);
                months[i-1] =yearAndMonths;
                calEnd.set(year,month,1);
                //(7)Calendar对象cal调用方法把天数减1,之后就是2月份的最后一天
                calEnd.add(Calendar.DAY_OF_MONTH, -1);
                dateCalendar = calEnd.getTime();
                String end = format.format(dateCalendar);

                Integer members =  memberDao.getMember(start,end);
                //  memberCount.add(sumReport);
                //查询之前的会员数量
                sumReport  =  memberDao.getMemberReport(start);
                //存入之前的会员数量
                memberCount[i-1]=sumReport+members;
         }
        }

        memberReport.setMonths(months);
        memberReport.setMemberCount(memberCount);
        return memberReport;
    }

    @Override
    public Member findById(Integer memberId) {
        Member member = memberDao.findById(memberId);
        return member;
    }


}
